package com.cc.controller;

import com.cc.entity.EbProduct;
import com.cc.entity.EbProductClass;
import com.cc.service.IEbProductClassService;
import com.cc.service.IEbProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/productClass")
public class EbProductClassController {

    @Autowired
    private IEbProductClassService iEbProductClassService;
    @Autowired
    private IEbProductService iEbProductService;

    @RequestMapping("/to_save")
    public String a(Model model) {
        List< EbProductClass> listt= iEbProductClassService.selectAll();
        model.addAttribute("listt",listt);
        return "manage/productClass-add";

    }

    @RequestMapping("/save_productClass")
    public String b(EbProductClass ebProductClass) {
        if((ebProductClass.getEpcname()==null||ebProductClass.getEpcname().trim().length()==0)&&
                (ebProductClass.getEpcparentid()==null)){
            return "manage/paramClassError";
        }
        int result=iEbProductClassService.save(ebProductClass);
        if(ebProductClass.getEpcparentid()==1){
            EbProductClass cc=iEbProductClassService.selects();
            iEbProductClassService.update(cc);

        }
        if(result<=0){
            return"manage/paramError";
        }
        return "manage/manage-result";

    }

    @RequestMapping("/list")
    public String c(Model model, @RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "pageSize",defaultValue = "6") int pageSize){
        List<EbProductClass> ebProCla=iEbProductClassService.findByPage(page,pageSize);
        System.out.println("ebprocla"+ebProCla.size());
        List<EbProductClass> listBig=new ArrayList<>();
        List<EbProductClass> listSmall=new ArrayList<>();
        for(EbProductClass c:ebProCla){
            if(c.getEpcid().compareTo(c.getEpcparentid())==0){
                listBig.add(c);
            }else{
               listSmall.add(c);
            }
        }
        model.addAttribute("listBig",listBig);
        model.addAttribute("listSmall",listSmall);
        model.addAttribute("curPage",page);
        int totalnum=iEbProductClassService.total();//总记录条数
        double no=(double)totalnum/pageSize;//总页数
        int num=(int)Math.ceil(no);//向上取整总页数
        model.addAttribute("totalpages",num);
        return "manage/productClass";
    }

    @RequestMapping("/editBig")
    public String d(Model model,int epcid){
        EbProductClass ebProductClass=iEbProductClassService.selectByCondition(epcid);
        model.addAttribute("ebProductClass",ebProductClass);
        return "manage/productBigClass-modify";
    }
    @RequestMapping("/editSmall")
    public String f(Model model,int epcid){
        EbProductClass ebProductClass=iEbProductClassService.selectByCondition(epcid);
        List< EbProductClass> list1= iEbProductClassService.selectAll();
        model.addAttribute("list1",list1);
        model.addAttribute("ebProductClass",ebProductClass);
        return "manage/productSmallClass-modify";
    }
    @RequestMapping("/editSubmit")
    public String e(EbProductClass ebProductClass){
        int result=iEbProductClassService.updateById(ebProductClass);
        if (result<=0){
            return "manage/paramError";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/deleteBig/{epcid}")
    public String g(@PathVariable("epcid") int id){
        List< EbProductClass> list2= iEbProductClassService.selectAll();
        for(EbProductClass c:list2){
            if((c.getEpcid().compareTo(c.getEpcparentid())!=0)&&(c.getEpcparentid()==id)){
                return"manage/paramClassError";
            }
        }
        int result=iEbProductClassService.delete(id);
        if(result==0){

            return"manage/paramError";
        }
        return "manage/manage-result";
    }
    @RequestMapping("/deleteSmall/{epcid}")
    public String h(@PathVariable("epcid") int id){
        List<EbProduct> list3;
        list3 = iEbProductService.selectAll();
        for(EbProduct d:list3){
            if((d.getEpcchildid()!=0)&&(id==d.getEpcchildid())){
                return"manage/paramError";
            }
        }
        int result=iEbProductClassService.delete(id);
        if(result==0){
            return"manage/paramError";
        }
        return "manage/manage-result";
    }


}
